--[[
    单个下注区域
]]--

local app = require "app"
local game = app.game.running
local Vector3 = CS.UnityEngine.Vector3

local M = game.ui.element()

function M:ctor(config, manager)
    self.id = config.id
    self.config = config
    self.manager = manager
end

function M:on_injected(bind)
    bind("trigger.enter", self.trigger, self.on_enter)
    bind("trigger.exit", self.trigger, self.on_exit)
    bind("trigger.up", self.trigger, self.on_up)
    bind("trigger.down", self.trigger, self.on_down)
end

function M:get_position()
    local transform = self.trigger.transform
    local rect = transform.rect
    local position = transform.position
    local offset_x = (math.random() - 0.5) * rect.width * 0.5
    local offset_y = (math.random() - 0.5) * rect.height * 0.5
    return position + Vector3(offset_x, offset_y, 0) * transform.lossyScale.x
end

function M:is_hit(no)
    for _, num in pairs(self.config.nums) do
        if no == num then
            return true
        end
    end
    return false
end

function M:on_enter()
    self.manager:on_enter(self)
end

function M:on_exit()
    self.manager:on_exit(self)
end

function M:on_up()
    self.manager:on_up(self)
end

function M:on_down()
    self.manager:on_press(self)
end

function M:on_select(is_pressed)
    self.animator:SetBool("pressed", is_pressed)
end

function M:on_hit(is_hit)
    if self.config.disable then
        return
    end
    
    self.animator:SetBool("hit", is_hit)
end

return M